#include "LoginPanel.h"

#include "MessengerPanel.h"

#include "MsnDef.h"
#include "MsnMessenger.h"

#include <wx/defs.h>
#include <wx/treectrl.h>
#include <wx/artprov.h>

//#include "wx/things/menubtn.h"

int ID_BUTTON_OK = wxNewId();
int ID_COMBO_STATUS = wxNewId();

BEGIN_EVENT_TABLE(LoginPanel, wxPanel)
    EVT_BUTTON(ID_BUTTON_OK, LoginPanel::OnOk)
    EVT_KEY_DOWN(LoginPanel::OnKeyDown)

    EVT_COMBOBOX(ID_COMBO_STATUS, LoginPanel::OnStatusChanged)
END_EVENT_TABLE()

LoginPanel::LoginPanel(MessengerPanel* mp)
: wxPanel(mp->leftPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER), messengerPanel(mp)
{
    //wxBitmap msnImage(_T("images/msn.png"), wxBITMAP_TYPE_PNG);
    //wxBitmap msnImage;
    //wxStaticBitmap* msnBitmap = new wxStaticBitmap(this, wxID_ANY, msnImage);

	userEmailLabel = new wxStaticText(this, wxID_ANY, _T(""));
	userPasswordLabel = new wxStaticText(this, wxID_ANY, _T(""));

    emailTextCtrl = new wxTextCtrl(this, wxID_ANY, _T(""),wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER|wxWANTS_CHARS);
    //emailTextCtrl = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize);

    passwordTextCtrl = new wxTextCtrl(this, 1999, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER|wxWANTS_CHARS|wxTE_PASSWORD);

	//TODO get default status 
    statusComboBox = new wxComboBox(this, ID_COMBO_STATUS,
            _T(""), wxDefaultPosition, wxDefaultSize, 0, NULL, wxCB_READONLY);


//    m_AnimationCtrl = new wxAnimationCtrl(this, wxID_ANY);
//    m_AnimationCtrl->LoadFile(_T("images/throbber.gif"));
    //m_AnimationCtrl->Play();

	connectLabel = new wxStaticText(this, wxID_ANY, wxEmptyString);

    okButton = new wxButton(this, ID_BUTTON_OK, _("Sign In"),wxDefaultPosition, wxSize(50, 30), wxNO_BORDER);

    emailTextCtrl->Connect(wxID_ANY, wxEVT_KEY_DOWN, (wxObjectEventFunction) (wxEventFunction) &LoginPanel::OnKeyDown, NULL, this);
    passwordTextCtrl->Connect(wxID_ANY, wxEVT_KEY_DOWN, (wxObjectEventFunction) (wxEventFunction) &LoginPanel::OnKeyDown, NULL, this);
    statusComboBox->Connect(wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED , (wxObjectEventFunction) (wxEventFunction) &LoginPanel::OnStatusChanged, NULL, this);

    emailTextCtrl->SetMinSize(wxSize(100, 20));
    emailTextCtrl->SetMaxSize(wxSize(200, 20));

    passwordTextCtrl->SetMinSize(wxSize(100, 20));
    passwordTextCtrl->SetMaxSize(wxSize(200, 20));

    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);

 //   sizer->Add(m_AnimationCtrl, 0, wxALL|wxALIGN_CENTER, 3);

    //sizer->Add(msnBitmap, 0, wxALL|wxALIGN_CENTER, 5);
    sizer->Add(userEmailLabel, 0, wxLEFT|wxTOP, 3);
    sizer->Add(emailTextCtrl, 0, wxALL|wxALIGN_CENTER | wxEXPAND, 3);
    sizer->Add(userPasswordLabel, 0, wxLEFT|wxTOP, 3);
    sizer->Add(passwordTextCtrl, 0, wxALL|wxALIGN_CENTER | wxEXPAND, 3);
    sizer->Add(statusComboBox, 0, wxALL, 3);

	sizer->Add(connectLabel, 0, wxALL, 5);

    sizer->Add(okButton, 0, wxALL|wxALIGN_CENTER, 5);

    SetSizer(sizer);

	//SetMaxSize(wxSize(200, 200)); 

	SetBackgroundColour(*wxWHITE);

	UpdateUIText();

	statusComboBox->SetSelection(0);

	//passwordTextCtrl->SetFocus();
}

LoginPanel::~LoginPanel()
{

}

void LoginPanel::UpdateUIText()
{
	userEmailLabel->SetLabel(_("Email:"));
	userPasswordLabel->SetLabel(_("Password:"));

	int selection = statusComboBox->GetSelection();

	statusComboBox->Clear();

	statusComboBox->Append(_("Online"));
	statusComboBox->Append(_("Busy"));
	statusComboBox->Append(_("Idle"));
	statusComboBox->Append(_("Be right back"));
	statusComboBox->Append(_("Away"));
	statusComboBox->Append(_("On the phone"));
	statusComboBox->Append(_("Out to lunch"));
	statusComboBox->Append(_("Hidden"));

	statusComboBox->SetSelection(selection);


	UpdateButtonText();
	UpdateStatusText();
}

void LoginPanel::UpdateStatusText()
{
	int status = messengerPanel->GetMessengerStatus();
	switch(status){
		case MESSENGER_CONNECTING: 
			connectLabel->SetLabel(_("Connecting..."));
			return;
		case MESSENGER_CONNECTION_FAILED:
			connectLabel->SetLabel(_("Can not connect to MSN server"));
			return;
		case MESSENGER_CONNECTION_BROKEN:
			connectLabel->SetLabel(_("Connection is broken"));
			return;
		case MESSENGER_AUTHENTICATION_FAILED:
			connectLabel->SetLabel(_("Can not login MSN server"));
			return;
		default:
			connectLabel->SetLabel(_T(""));
	}
}

void LoginPanel::UpdateButtonText()
{
	int status = messengerPanel->GetMessengerStatus();

	if(status == MESSENGER_CONNECTING){
		okButton->SetLabel(_("Cancel"));
	}else{
		okButton->SetLabel(_("Sign In"));
	}	
}

void LoginPanel::UpdateUI()
{
	int status = messengerPanel->GetMessengerStatus();

	if(status == MESSENGER_CONNECTING){
		emailTextCtrl->Enable(false);
		passwordTextCtrl->Enable(false);
		statusComboBox->Enable(false);
	}else{
		emailTextCtrl->Enable(true);
		passwordTextCtrl->Enable(true);
		statusComboBox->Enable(true);
	}

	UpdateButtonText();	
	UpdateStatusText();
}


void LoginPanel::OnStatusChanged(wxCommandEvent& event)
{
   //wxMessageBox(m_Status->GetValue());
}



void LoginPanel::OnOk(wxCommandEvent& event)
{
	int status = messengerPanel->GetMessengerStatus();

	if(status == MESSENGER_CONNECTING){
		OnCancel();
	}else{
		OnSignIn();
	}

	UpdateButtonText();
}

void LoginPanel::OnSignIn()
{
    wxString email = emailTextCtrl->GetValue();
    wxString passwrod = passwordTextCtrl->GetValue();

    if(email.IsEmpty()){
        emailTextCtrl->SetFocus();
        return;
    }

    if(passwrod.IsEmpty()){
        passwordTextCtrl->SetFocus();
        return;
    }

     wxString status = MsnDef::STATUS_CODE_ARRAY[statusComboBox->GetCurrentSelection()];
	messengerPanel->SignIn(email, passwrod, status);

	UpdateStatusText();
	UpdateUI();
}

void LoginPanel::OnCancel()
{
    messengerPanel->SignOut();

	UpdateStatusText();

    UpdateUI();
}

void LoginPanel::OnKeyDown(wxKeyEvent& event)
{
    const wxChar ch = event.GetKeyCode();

    if(ch == WXK_RETURN ){
        OnSignIn();
    }else{
        event.Skip();
    }
}
